<template>
	<view>
		<scroll-view class="scroll" scroll-y="true">
			<view v-for="item in infoList" @click="changeContactId" :data-index="item.contactId" :key='key'>
				<view>
					<view v-if="item.contactId==currentId" class="iconfont icon-gouxuanzhong iconBox"></view>
					<view :class="item.contactId==currentId?'change':''" class="product-name-wrap">
						<view class="ui-list-item-time">
							<p>{{item.address}}</p>
							<p class="iconfont icon-shanchu ui-list-item-del" :data-id="item.contactId"
								@tap.stop="deleteInfo"></p>
						</view>
						<view class="ui-list-item-address">
							{{item.company}}
						</view>
						<view class="ui-list-item-info">{{item.linkman}}
							<text>&nbsp;&nbsp;</text> {{item.phone}}
							<text @tap.stop="modifyInfo" :data-id="item.contactId" :data-Info="item"
								class="iconfont icon-xiugai modify"></text>
						</view>
						<view class="separate"></view>
					</view>
				</view>

			</view>
		</scroll-view>
		<view class="add-address">
			<!-- <image class="add-img" src="../../icons/cancel.png"></image> -->
			<u-button  @click="addUserInfo">新增信息</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				openId: '',
				//信息列表
				infoList: [],
				currentId: 0,
			}
		},
		onLoad(option) {
			console.log(option);
			this.currentId=option.nextContactId;
			this.openId = uni.getStorageSync('openId');
		},
		onShow() {
			this.getContactId();
		},
		methods: {
			//addUserInfo
			addUserInfo(){
				uni.navigateTo({
					url:'../setInfo/setInfo'
				})
			},
			// 获取用户已经设置好的数据
			getContactId() {
				let that = this;
				uni.request({
					url: 'https://zioncloud.top/repair/contact/getContact',
					method: 'POST',
					name: 'getAllInfo',
					header: {
						"Content-Type": "application/json"
					},
					data: {
						openId: that.openId
					},
					success(res) {
						that.infoList = res.data.data.contact;
						console.log(that.infoList);
					}
				})
			},
			//切换选中的信息
			changeContactId(e) {
				let that = this;
				let contactId = e.currentTarget.dataset.index;
				uni.setStorageSync('contasctId', contactId);
				console.log(contactId);
				//将当前选择的contactId 告诉后台
				uni.request({
					url: "https://zioncloud.top/repair/user/changeContact",
					method: 'POST',
					header: {
						"Content-Type": "application/json"
					},
					data: {
						openId: that.openId,
						contactId: contactId
					},
					success(res) {
						console.log('成功获取')
						that.currentId = contactId;
					}
				})
				// 保存当前选择的信息到缓存
				uni.request({
					url: "https://zioncloud.top/repair/contact/getContact",
					method: 'POST',
					header: {
						"Content-Type": "application/json"
					},
					data: {
						openId: that.openId,
						contactId: contactId
					},
					success(res) {
						console.log(res);
						let arrContact = res.data.data.contact;
						console.log(arrContact);
						arrContact.forEach(function(value, index) {
							if (value.contactId == contactId) {
								console.log(value);
								uni.setStorageSync('dataInfo', value);
							}
						})
					}
				})
			},
			//删除信息
			deleteInfo(e) {
				let that = this;
				console.log(e);
				let current = e.currentTarget.dataset.id;
				uni.showModal({
					title: '提醒',
					content: '确定要删除信息吗?',
					success: function(res) {
						//如果用户点击确定
						if (res.confirm) {
							console.log('用户点击确定');
							uni.request({
								url: 'https://zioncloud.top/repair/contact/delete',
								name: 'text',
								method: 'POST',
								name: 'delete',
								header: {
									"Content-Type": "application/json"
								},
								data: {
									contactId: current,
									openId: that.openId
								},
								success(res) {
									console.log(res);
									that.getContactId();
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			// 修改信息
			modifyInfo(e) {
				let that = this;
				let current = e.currentTarget.dataset.id;
				let modifyInfo = [];
				that.infoList.forEach(function(value, index) {
					if (value.contactId == current) {
						modifyInfo = value;
					}
				})
				let showId = true;
				uni.navigateTo({
					url: '../setInfo/setInfo?modifyId=' + modifyInfo.contactId + '&showId=' + showId + '&address=' + modifyInfo.address + '&company=' + modifyInfo.company + '&linkman=' + modifyInfo.linkman +
						'&phone=' + modifyInfo.phone + '&department=' + modifyInfo.department
				})
			}
		}
	}
</script>

<style>
	page {
		display: flex;
		flex-direction: column;
		height: 100%;
	}

	.product-name-wrap {
		position:relative;
		margin: 0px 10px !important;
		font-size: 14px;
		color: #404040;
	}

	.ui-list-item-info {
		margin: 5px 0px;
	}

	.ui-list-item-address {
		color: #585c64;
		/* font-size: 35rpx; */
		font-weight: bolder;
	}

	.ui-list-item-time {
		margin: 5px 0px;
	}

	.ui-list-item-del {
		position: absolute;
		top:0;
		right: 0px;
		color: #585c64;
	}

	/* 分割线 */
	.separate {
		margin: 5px 0px;
		height: 2rpx;
		background-color: #f2f2f2;
	}

	.add-address {
		margin: 0 auto;
		margin-top: 30px;
		width: 150px;
		height: 35px;
		line-height: 35px;
		text-align: center;
		color: #000;
		border-radius: 5rpx;
		display: block;
	}

	.add-img {
		margin-right: 15rpx;
		width: 15px;
		height: 15px;
	}

	/* 修改 */
	.modify {
		float: right;
	}

	/* 显示盒子 */
	.infoBox {
		background-color: #CCC;
		/* height: 200rpx; */
	}

	/* 勾选图标 */
	.iconBox {
		float: left;
		height: 150rpx;
		line-height: 150rpx;
		width: 10%;
		color: green;
		text-align: center;
		font-size: 35rpx;
		/* background-color: red; */
	}

	.change {
		/* float: right; */
		width: 95%;
		margin:0 10px !important;
	}

	.noneShow {
		height: 150rpx;
		width: 5%
	}
</style>
